package com.pipayshop.controller;

import com.pipayshop.domain.StoreCommodityHistory;
import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.service.PhysicalStoreCommodityHistoryService;
import com.pipayshop.service.PhysicalStoreCommodityInfoService;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 实体店用户商品历史记录表 前端控制器
 * </p>
 *
 * @author zxb
 * @since 2023-07-29
 */
@RestController
@Slf4j
@Api(value = "实体店用户商品历史记录表",tags = "实体店用户商品历史记录表")
@RequestMapping("/pipayshopapi/store-commodity-history")
public class PhysicalStoreCommodityHistoryController {
    @Resource
    private PhysicalStoreCommodityHistoryService shopCommodityHistoryService;

    @Resource
    private PhysicalStoreCommodityInfoService shopCommodityService;
    @PostMapping("deleteHistory")
    @ApiOperation("删除用户浏览商品的历史记录-实体店")
    public ResponseVO deleteHistory(String userId,
                                    String commodityId) {

        try {
            boolean flag = shopCommodityHistoryService.deleteHistory(userId, commodityId);
            if (!flag) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("delete_success");
        } catch (Exception e) {
            // log.error(e.getMessage());
            throw new BusinessException("delete_failed");
        }
    }
    @PostMapping("addHistory")
    @ApiOperation("新增用户浏览网店商品的历史记录")
    public ResponseVO addHistory(StoreCommodityHistory shopCommodityHistory){
        try {
            shopCommodityHistoryService.addHistory(shopCommodityHistory);
            return ResponseVO.getSuccessResponseVo("新增用户浏览商品的历史记录成功-实体店");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("add_browsing_history_failed");
        }
    }
    @GetMapping("history/{page}/{limit}/{userId}")
    @ApiOperation("根据用户id查询用户浏览商品历史-实体店")
    public ResponseVO<PageDataVO> historyList(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable("userId") String userId) {
        try {
            PageDataVO pageDataVO = shopCommodityService.historyList(page, limit, userId);
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("query_failed_browsing_history_failed");
        }
    }
}
